package com.financing.service.finance;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.financing.enums.finance.FinanceLogType;
import com.financing.model.finance.FinanceLog;
import com.financing.model.user.User;
import com.financing.service.BaseService;
import com.financing.util.Str;
import com.jfinal.plugin.activerecord.Page;

public class FinanceLogService extends BaseService<FinanceLog>{
	
	/**
	 * 新增账务记录
	 */
	public static void build(User from, User to, double amount, String code, String type, String summary) {
		if(amount>0) {
			FinanceLog log = new FinanceLog();
			log.setCreateTime(new Date());
			log.setSummary(summary);
			log.setAmount(amount);
			log.setType(type);
			log.setCode(code);
			if(Str.eq(FinanceLogType.DEPOSIT, type)){
				log.setFrom(0);
				log.setFromRealName("富有");
				log.setFromBalance("0.00");
			}else {
				log.setFrom(from.getId());
				log.setFromRealName(from.getRealName());
				log.setFromBalance(String.valueOf(from.getAvailableAmount()));
			}
			if(Str.eq(FinanceLogType.DEPOSIT_FEE, type) || Str.eq(FinanceLogType.WITHDRAW_FEE, type)){
				log.setTo(0);
				log.setToRealName("富有");
				log.setToBalance("0.00");
			}else{
				log.setTo(to.getId());
				log.setToRealName(to.getRealName());
				log.setToBalance(String.valueOf(to.getAvailableAmount()));
			}
			log.save();
		}
	}
	
	/**
	 * 分页数据
	 */ 
	public Page<FinanceLog> list(int page, int size, Map<String, String> map) {
		return FinanceLog.me.paginate(page, size, buildSql("finance_log",Arrays.asList("role", "user_id", "mobile", "create_time", "type"),map));
	}
}
